# coding=utf-8

from playerMove import *
from shortestMoveStra import *
import numpy as np

def boardUpdate(board, move1, move2):
   print(board[2])
   for i in range(2):
       if board[1][i][3] == 1:
           if move1 == 0:
               for k in range(2):
                   if board[2][k][0] == 1:
                       score = board[2][k][1]
                       scoreO = board[2][k][1]
                       kt = k
               xOld = board[1][i][0]
               yOld = board[1][i][1]

               board[1][i][1] = board[1][i][1] + 1

               if board[0][(board[1][i][0], board[1][i][1])][0] == 1:
                   score += (board[0][(board[1][i][0], board[1][i][1])][1])

               board[0][(xOld, yOld)] = (3, 0)
               board[0][(board[1][i][0], board[1][i][1])] = (4,1)

               board[2][kt] = (1, score, scoreO)

           elif move1 == 1:
               for k in range(2):
                   if board[2][k][0] == 1:
                        score = board[2][k][1]
                        scoreO = board[2][k][1]
                        kt = k
               xOld = board[1][i][0]
               yOld = board[1][i][1]
               board[1][i][1] = board[1][i][1] - 1
               if board[0][(board[1][i][0], board[1][i][1])][0] == 1:
                    score += (board[0][(board[1][i][0], board[1][i][1])][1])
               board[0][(xOld, yOld)] = (3, 0)
               board[0][(board[1][i][0], board[1][i][1])] = (4, 1)
               board[2][kt] = (1, score, scoreO)

           elif move1 == 2:
               for k in range(2):
                   if board[2][k][0] == 1:
                        score = board[2][k][1]
                        scoreO = board[2][k][1]
                        kt = k

               xOld = board[1][i][0]
               yOld = board[1][i][1]

               board[1][i][0] = board[1][i][0] - 1

               if board[0][(board[1][i][0], board[1][i][1])][0] == 1:
                   score += (board[0][(board[1][i][0], board[1][i][1])][1])
               board[0][(xOld, yOld)] = (3, 0)
               board[0][(board[1][i][0], board[1][i][1])] = (4, 1)
               board[2][kt] = (1, score, scoreO)

           elif move1 == 3:
               for k in range(2):
                   if board[2][k][0] == 1:
                       score = board[2][k][1]
                       scoreO = board[2][k][1]
                       kt = k

               xOld = board[1][i][0]
               yOld = board[1][i][1]
               #print(board[1][i])
               board[1][i][0] = board[1][i][0] + 1

               if board[0][(board[1][i][0], board[1][i][1])][0] == 1:
                   score += (board[0][(board[1][i][0], board[1][i][1])][1])
               board[0][(xOld, yOld)] = (3, 0)
               board[0][(board[1][i][0], board[1][i][1])] = (4, 1)
               board[2][kt] = (1, score, scoreO)

       elif board[1][i][3] == 2:
           if move2 == 0:
               for k in range(2):
                   if board[2][k][0] == 2:
                       score = board[2][k][1]
                       scoreO = board[2][k][1]
                       kt = k
               xOld = board[1][i][0]
               yOld = board[1][i][1]

               board[1][i][1] = board[1][i][1] + 1
               if board[0][(board[1][i][0], board[1][i][1])][0] == 1:
                   score += (board[0][(board[1][i][0], board[1][i][1])][1])

               board[0][(xOld, yOld)] = (3, 0)
               board[0][(board[1][i][0], board[1][i][1])] = (4, 2)
               board[2][kt] = (2, score, scoreO)

           elif move2 == 1:
               for k in range(2):
                   if board[2][k][0] == 2:
                        score = board[2][k][1]
                        scoreO = board[2][k][1]
                        kt = k
               xOld = board[1][i][0]
               yOld = board[1][i][1]
               board[1][i][1] = board[1][i][1] - 1
               if board[0][(board[1][i][0], board[1][i][1])][0] == 1:
                    score += (board[0][(board[1][i][0], board[1][i][1])][1])
               board[0][(xOld, yOld)] = (3, 0)
               board[0][(board[1][i][0], board[1][i][1])] = (4, 2)
               board[2][kt] = (2, score, scoreO)

           elif move2 == 2:
               for k in range(2):
                   if board[2][k][0] == 2:
                       score = board[2][k][1]
                       scoreO = board[2][k][1]
                       kt = k

               xOld = board[1][i][0]
               yOld = board[1][i][1]

               board[1][i][0] = board[1][i][0] - 1

               if board[0][(board[1][i][0], board[1][i][1])][0] == 1:
                   score += (board[0][(board[1][i][0], board[1][i][1])][1])
               board[0][(xOld, yOld)] = (3, 0)
               board[0][(board[1][i][0], board[1][i][1])] = (4, 2)
               board[2][kt] = (2, score, scoreO)

           elif move2 == 3:
               for k in range(2):
                   if board[2][k][0] == 2:
                       score = board[2][k][1]
                       scoreO = board[2][k][1]
                       kt = k
               xOld = board[1][i][0]
               yOld = board[1][i][1]

               board[1][i][0] = board[1][i][0] + 1

               if board[0][(board[1][i][0], board[1][i][1])][0] == 1:
                   score += (board[0][(board[1][i][0], board[1][i][1])][1])
               board[0][(xOld, yOld)] = (3, 0)
               board[0][(board[1][i][0], board[1][i][1])] = (4, 2)
               board[2][kt] = (2, score, scoreO)

   if (board[1][1][0] == board[1][0][0]) and (board[1][1][1] == board[1][0][1]):
       for k in range(2):
           if board[2][k][0] == 1:
               score = board[2][k][1]
               scoreO = board[2][k][2]
               scoreTemp = score -scoreO
               k1 = k
       #board[2][kt] = (1,  scoreO + scoreTemp / 2.0, scoreO)

       for k in range(2):
           if board[2][k][0] == 2:
               score2 = board[2][k][1]
               scoreO2 = board[2][k][2]
               k2 = k

       if (score - scoreO) > 0:
           scoreTemp = score - scoreO
       else:
           scoreTemp = score2 - scoreO2

       board[2][k1] = (1, scoreO + scoreTemp / 2.0, scoreO)
       board[2][k2] = (2, scoreO2 + scoreTemp / 2.0, scoreO2)



   for key in board[0]:
       if board[0][key][0] == 1 and board[0][key][1] > 0:
           if (board[0][key][1] - 1) > 0:
                board[0][key] = (1, board[0][key][1] - 1)
           else:
               board[0][key] = (3, 0)

   for key in board[0]:
       if board[0][key][0] == 3:
           index = np.random.choice(2, 1, replace=False, p=[0.005, 0.995])[0] + 1
           if index == 1:
               score = np.random.randint(20, 50)
               board[0][key] = (1, score)

   return board


def gamingBoard(board):

    m1 = player1Greedy(board, 1)
    #m1 = player1(board)
    #m2 = player2(board)
    m2 = player2Greedy(board, 2)

    board = boardUpdate(board, m1, m2)

    return board

